/*
 * @lc app=leetcode.cn id=636 lang=javascript
 *
 * [636] 函数的独占时间
 */

// @lc code=start
/**
 * @param {number} n
 * @param {string[]} logs
 * @return {number[]}
 */
var exclusiveTime = function(n, logs) {
  // let stack = [];
  // let result =[]
  // let last = 0;
  // for(let i =0; i<logs.length; i++){
  //   let [id, opt, val] = logs[i].split(':');
  //   if(!result[id]){ result[id] = 0 };
  //   console.log(val, stack)
  //   if(opt === 'end'){
  //     result[id] += val - stack.pop() + 1 - last;
  //     last = result[result.length-1] || 0;
  //   } else{
  //     stack.push(val)
  //     last = 0;
  //   }
  // }
  // console.log(result)
  // return result;

  let stack = [];
  let result = new Array(n).fill(0);
  let pre = 0;
  for(let i =0; i<logs.length; i++){
    let [id, opt, val] = logs[i].split(':');
    console.log(pre, result)
    if(opt === 'start'){
      result[stack[stack.length-1 || 0]] += Number(val) - pre;
      stack.push(id);
    } else {
      result[stack[stack.length-1]] += Number(val) - pre + 1;
      stack.pop()
    }
    pre = Number(val);
  }
  return result;

};
// @lc code=end

